package com.teach.controller.shop;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teach.R;
import com.teach.dto.LoginUser;
import com.teach.dto.ShopOrderSearch;
import com.teach.entity.Comments;
import com.teach.entity.ShopOrderItem;
import com.teach.mapper.ShopOrderItemMapper;
import com.teach.service.CommentsService;
import com.teach.service.shop.ShopAddressService;
import com.teach.service.shop.ShopOrederService;

import lombok.extern.slf4j.Slf4j;

@Controller
public class ShopOrderController {
	@Autowired
	private ShopOrederService service;
	
	@Autowired
	private ShopAddressService service1;
	
	@Autowired
	private CommentsService service2;
	
	@Autowired
	private ShopOrderItemMapper mapper;
	
	//订单分页查询
	@RequestMapping("/sss-{current}.do")
	public String doOrderAll(LoginUser user,Integer type,Model model,@PathVariable Integer current) {
		ShopOrderSearch search=new ShopOrderSearch();
		search.setShopOrderState(type);
		search.setShopOrderUserName(user.getUserName());
		search.setCurrent(current);
		model.addAttribute("orderlist", service.page(search));
		model.addAttribute("type", type);
		//return service.page(search);
		return "shop/userMain";
	}
	
	//取消订单
	@PostMapping("/shop-order-delect.do")
	@ResponseBody
	public Integer delectby(Long orederID) {
		service.delete(orederID);
		return 1;
	}
	
	//订单支付
	@GetMapping("/oreder-tijiao.do")
	public String buy(Model model,LoginUser user,Long orderId) {
		model.addAttribute("addressList", service1.get(user.getUserName()));
		model.addAttribute("order",service.select(orderId));
		model.addAttribute("total", service.select(orderId).getShopOrderItemTotalPrice());
		model.addAttribute("orderid", service.select(orderId).getShopOrderId());
		return "shop/order";
	}
	
	@PostMapping("/update-order-state.do")
	@ResponseBody
	public Integer xiugaizhuangtai(Integer state,Long orderId) {
		service.update(state+1, orderId);
		if(state==3) {
			ShopOrderItem order =new ShopOrderItem();
			order.setShopOrderItemTakeTime(new Date());
			order.setShopOrderItemId(orderId);
			mapper.updateByPrimaryKeySelective(order);
		}
		return 1;
	}
	
	//添加商品评论
	@PostMapping("/shop-add-pinlun.do")
	@ResponseBody
	public Object tjiaplun(HttpServletRequest request,Integer pf,Long productId,String pinlun,Integer state,Long px) {
	try {
		Comments comments=new Comments();
		LoginUser user=(LoginUser) request.getSession().getAttribute("user");
		comments.setCommentUserName(user.getUserName());
		comments.setCommentType("商品");
		comments.setCommentCommentator(productId);
		comments.setCommentScore(pf);
		comments.setCommentContent(pinlun);
		String remoteAddr = request.getRemoteAddr();
	    String forwarded = request.getHeader("X-Forwarded-For");
	    String realIp = request.getHeader("X-Real-IP");
	    String ip = null;
	    if (realIp == null) {
	        if (forwarded == null) {
	            ip = remoteAddr;
	        } else {
	            ip = remoteAddr + "/" + forwarded.split(",")[0];
	        }
	    } else {
	        if (realIp.equals(forwarded)) {
	            ip = realIp;
	        } else {
	            if(forwarded != null){
	                forwarded = forwarded.split(",")[0];
	            }
	            ip = realIp + "/" + forwarded;
	        }
	    }
		comments.setCommentIp(ip);
		service2.commentsInsert(comments);
		service.update(state+1, px);
		return 1;
	}catch(Exception e) {
		return R.error("发表评论失败");
	}	
	}
}
